package com.struggle.myrabbitmq.sample.direct;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.struggle.myrabbitmq.sample.MyRabbitConstants;
import com.struggle.myrabbitmq.util.ConnectionUtil;
import lombok.SneakyThrows;

import java.io.IOException;

/**
 * @className Send
 * @Description 路由模式：需要将一个队列绑定到交换机上，要求该消息与一个特定的路由键完全匹配
 * @Author liuwj3
 * @Date 2019/10/8 0008 上午 10:09
 **/
public class Send {
    /**
     * 1.一般情况可以使用rabbitMQ自带的Exchange：”"(该Exchange的名字为空字符串，下文称其为default Exchange)。
     *
     * 2.这种模式下不需要将Exchange进行任何绑定(binding)操作
     *
     * 3.消息传递时需要一个“RouteKey”，可以简单的理解为要发送到的队列名字。
     *
     * 4.如果vhost中不存在RouteKey中指定的队列名，则该消息会被抛弃。
     * @param args
     */
    @SneakyThrows
    public static void main(String[] args) throws IOException {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        // 声明路由模式交换机
        channel.exchangeDeclare(MyRabbitConstants.EXCHANGE_NAME_DIRECT, BuiltinExchangeType.DIRECT);

        String message = "删除商品";
        // 指定消息的key为delete
        channel.basicPublish(MyRabbitConstants.EXCHANGE_NAME_DIRECT, "delete", null, message.getBytes());
        channel.basicPublish(MyRabbitConstants.EXCHANGE_NAME_DIRECT, "insert", null, "新增商品".getBytes());
        System.out.println(" [x] Sent '" + message + "'");
        System.out.println(" [x] Sent '新增商品'");
        channel.close();
        connection.close();
    }
}
